package main

import (
	"git.woa.com/polaris/polaris-go/v2/api"
	"log"
	"net/http"
	_ "net/http/pprof"
)

const logLevel = api.InfoLog

// 主入口函数
func main() {
	go func() {
		log.Println(http.ListenAndServe("localhost:6070", nil))
	}()

	var err error
	err = api.ConfigLoggers("polaris", logLevel)
	if nil != err {
		log.Fatalf("fail to SetLogLevel, err is %v", err)
	}
	namespace := "Polaris"
	service := "polaris.helloworld"
	token := "nu/0WRA4EqSR1FagrjRj0fZwPXuGlMpX+zCuWu4uMqy8xr1vRjisSbA25aAC3mtU8MeeRsKhQiDAynUR09I="
	ip := "172.30.63.45"
	port := 8091
	// 获取默认配置
	// 一般用户进程只需要创建一个provider,重复使用即可
	provider, err := api.NewProviderAPIByFile("polaris.yaml")
	if nil != err {
		log.Fatalf("fail to create provider api by default config file, err %v", err)
	}
	// 注册服务
	request := &api.InstanceRegisterRequest{}
	request.Namespace = namespace
	request.Service = service
	request.ServiceToken = token
	request.Host = ip
	request.Port = port
	request.SetTTL(3)
	request.Metadata = map[string]string{
		"internal-registry-replace": "true",
		"a":                         "a",
		"b":                         "b",
	}
	resp, err := provider.Register(request)
	if nil != err {
		log.Fatalf("fail to register instance, err %v", err)
	}
	log.Printf("success to register instance, id is %s", resp.InstanceID)

	// 进程退出时销毁provider
	provider.Destroy()
}
